package com.chuiyan.topnews;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class Home extends Fragment {
    private LinearLayout search;
    private ArrayList<JSONObject> jsonObjects;
    private List<Fragment> list;
    private ViewPager myViewPager;
    private com.chuiyan.topnews.TabFragmentPagerAdapter adapter;
    private TextView type1;
    private TextView type2;
    private TextView type3;
    private TextView type4;
    private TextView type5;
    private TextView type6;
    private TextView type7;
    private TextView type8;
    private HorizontalScrollView horizontalScrollView;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.home,container,false);
//        initView();
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        search = getView().findViewById(R.id.search);
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),History.class);
                startActivity(intent);
            }
        });
    }

    public void initData() throws JSONException {
        for(int i=0;i<10;i++)
        {
            //JsonData data = new JsonData();
            //data.setTitle("这是一个标题");
            //data.setImage(R.drawable.photo);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("title","习近平：贯彻新发展理念推动高质量发展，奋力开创中部地区崛起新局面");
            jsonObject.put("image",1);
            jsonObjects.add(jsonObject);
        }
    }

    /*
    * 后端请求模板
    * */

    public void initDatatemp() throws JSONException {
        Map<String, String> map = new HashMap<>();
        map.put("type","1");
        if(jsonObjects.size()==0)
        {
            //1.创建请求队列
            InitQueue.queue = Volley.newRequestQueue(getContext());
            //2.创建post请求
            CustomRequest jsonObjectRequest = new CustomRequest(Request.Method.POST, "http://10.0.2.2:8080/news", map, new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    //Toast.makeText(getContext(), "成功了", Toast.LENGTH_SHORT).show();
                    //System.out.println(response);
                    JSONArray jsonArray = new JSONArray();
                    try {
                        jsonArray = response.getJSONArray("newsList");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    for(int i=0;i<jsonArray.length();i++)
                    {
                        try {
                            jsonObjects.add(jsonArray.getJSONObject(i));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }

            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    Toast.makeText(getContext(), "失败了", Toast.LENGTH_SHORT).show();
                }
            });
            //3.添加请求队列
            jsonObjectRequest.setTag("get_list");
            InitQueue.getHttpQueue().add(jsonObjectRequest);
        }

    }
    private void initView() {
        type1 = (TextView) getView().findViewById(R.id.type1);
        type2 = (TextView) getView().findViewById(R.id.type2);
        type3 = (TextView) getView().findViewById(R.id.type3);
        type4 = (TextView) getView().findViewById(R.id.type4);
        type5 = (TextView) getView().findViewById(R.id.type5);
        type6 = (TextView) getView().findViewById(R.id.type6);
        type7 = (TextView) getView().findViewById(R.id.type7);
        type8 = (TextView) getView().findViewById(R.id.type8);
        horizontalScrollView = getView().findViewById(R.id.typeScroll);
        myViewPager = (ViewPager) getView().findViewById(R.id.myViewPager);
        //绑定点击事件
        myViewPager.setOnPageChangeListener(new MyPagerChangeListener());
        //把Fragment添加到List集合里面
        list = new ArrayList<>();
        list.add(new com.chuiyan.topnews.FragmentDemo("type1"));
        list.add(new com.chuiyan.topnews.FragmentDemo("type2"));
        list.add(new com.chuiyan.topnews.FragmentDemo("type3"));
        list.add(new com.chuiyan.topnews.FragmentDemo("type4"));
        list.add(new com.chuiyan.topnews.FragmentDemo("type5"));
        list.add(new com.chuiyan.topnews.FragmentDemo("type6"));
        list.add(new com.chuiyan.topnews.FragmentDemo("type7"));
        list.add(new com.chuiyan.topnews.FragmentDemo("type8"));
        adapter = new com.chuiyan.topnews.TabFragmentPagerAdapter(getFragmentManager(), list);
        myViewPager.setAdapter(adapter);
        myViewPager.setCurrentItem(0);  //初始化显示第一个页面

    }

    /**
     * 设置一个ViewPager的侦听事件，当左右滑动ViewPager时菜单栏被选中状态跟着改变
     */
    public class MyPagerChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageSelected(int arg0) {
            switch (arg0) {//状态改变时底部对应的字体颜色改变,
                case 0:
                    type1.setTextColor(Color.GREEN);
                    type2.setTextColor(Color.BLACK);
                    type3.setTextColor(Color.BLACK);
                    type4.setTextColor(Color.BLACK);
                    type5.setTextColor(Color.BLACK);
                    type6.setTextColor(Color.BLACK);
                    type7.setTextColor(Color.BLACK);
                    type8.setTextColor(Color.BLACK);
                    break;
                case 1:
                    type1.setTextColor(Color.BLACK);
                    type2.setTextColor(Color.GREEN);
                    type3.setTextColor(Color.BLACK);
                    type4.setTextColor(Color.BLACK);
                    type5.setTextColor(Color.BLACK);
                    type6.setTextColor(Color.BLACK);
                    type7.setTextColor(Color.BLACK);
                    type8.setTextColor(Color.BLACK);
                    break;
                case 2:
                    type1.setTextColor(Color.BLACK);
                    type2.setTextColor(Color.BLACK);
                    type3.setTextColor(Color.GREEN);
                    type4.setTextColor(Color.BLACK);
                    type5.setTextColor(Color.BLACK);
                    type6.setTextColor(Color.BLACK);
                    type7.setTextColor(Color.BLACK);
                    type8.setTextColor(Color.BLACK);
                    break;
                case 3:
                    type1.setTextColor(Color.BLACK);
                    type2.setTextColor(Color.BLACK);
                    type3.setTextColor(Color.BLACK);
                    type4.setTextColor(Color.GREEN);
                    type5.setTextColor(Color.BLACK);
                    type6.setTextColor(Color.BLACK);
                    type7.setTextColor(Color.BLACK);
                    type8.setTextColor(Color.BLACK);
                    break;
                case 4:
                    type1.setTextColor(Color.BLACK);
                    type2.setTextColor(Color.BLACK);
                    type3.setTextColor(Color.BLACK);
                    type4.setTextColor(Color.BLACK);
                    type5.setTextColor(Color.GREEN);
                    type6.setTextColor(Color.BLACK);
                    type7.setTextColor(Color.BLACK);
                    type8.setTextColor(Color.BLACK);
                    break;
                case 5:
                    type1.setTextColor(Color.BLACK);
                    type2.setTextColor(Color.BLACK);
                    type3.setTextColor(Color.BLACK);
                    type4.setTextColor(Color.BLACK);
                    type5.setTextColor(Color.BLACK);
                    type6.setTextColor(Color.GREEN);
                    type7.setTextColor(Color.BLACK);
                    type8.setTextColor(Color.BLACK);
                    break;
                case 6:
                    type1.setTextColor(Color.BLACK);
                    type2.setTextColor(Color.BLACK);
                    type3.setTextColor(Color.BLACK);
                    type4.setTextColor(Color.BLACK);
                    type5.setTextColor(Color.BLACK);
                    type6.setTextColor(Color.BLACK);
                    type7.setTextColor(Color.GREEN);
                    type8.setTextColor(Color.BLACK);
                    break;
                case 7:
                    type1.setTextColor(Color.BLACK);
                    type2.setTextColor(Color.BLACK);
                    type3.setTextColor(Color.BLACK);
                    type4.setTextColor(Color.BLACK);
                    type5.setTextColor(Color.BLACK);
                    type6.setTextColor(Color.BLACK);
                    type7.setTextColor(Color.BLACK);
                    type8.setTextColor(Color.GREEN);
                    break;
            }
        }
    }


}
